<?php
/**
 * Create date    : 04/10/2012
 * Module         : admin
 * Model          : user
 * Version update : 04/10/2012
 */
class Admin_UserController extends Me_Controller_Action
{
    public function init()
    {
            parent::init();
            $this->_Model = new Admin_Model_User;//Create new object
            $this->view->baseUrl = $this->_request->getBaseUrl();
    }

    /**
     * Create date    : 04/10/2012
     * Function name indexAction
     * Description    : List all items
     * Version update : 04/10/2012  
     */
    public function indexAction()
    {
        $data = $this->_Model->listAll()->toArray();
        $this->view->data = $data;
    }//=========================================================================

    
    /**
     * Create date    : 04/10/2012
     * Function name indexAction
     * Description    : List all items
     * Version update : 04/10/2012  
     */
    public function deleteAction()
    {
            //Create object
            $id=$this->_request->getParam("id");
            $newModel = new Admin_Model_User;//Create new object
            $data = $newModel->deleteUser($id);
            $this->_redirect("admin/user/index");
            $this->_helper->viewRenderer->setNoRender();
    }//=========================================================================

    /**
      * 	ADD ACTION
      */
    public function addAction()
    {
            $newModel = new Admin_Model_User;//Create new object
            //========================Create captcha===========================
            $captcha = new Zend_Captcha_Image();

            $captcha->setTimeout('300')
                       ->setWordlen('6')
                       ->setHeight('80')
                       ->setWidth('200')
                       ->setImgDir(APPLICATION_PATH."/../public/captcha/images/")
                       ->setImgUrl($this->view->baseUrl."/public/captcha/images/")
                       ->setFont("./public/captcha/font/arial.ttf");

            //Send captcha to view	   
            $captcha->generate();
            $this->view->captcha = $captcha->render();
            $this->view->captchaID = $captcha->getId();

            //Create session to send confirm code
            $captchaSession = new Zend_Session_Namespace("Zend_Form_Captcha_".$captcha->getId());
            $captchaSession->word = $captcha->getWord();
            //========================END captcha===========================

            if( $this->_request->isPost() )
            {
                    //Get info confirm code from session
                    $captchaID = $this->_request->captcha_id;
                    $captchaSession = new Zend_Session_Namespace("Zend_Form_Captcha_".$captchaID);
                    $captchaWord = $captchaSession->word;


                    $data = array(
                            "user_email" => "".$this->_request->getPost("user_email")."",
                            "user_address" => "".$this->_request->getPost("user_address")."",
                            "user_level" => "".$this->_request->getPost("user_level")."",
                            "user_phone" => "".$this->_request->getPost("user_phone")."",
                            "user_password" => "".$this->_request->getPost("user_password").""
                    );
                    $newModel->addUser($data);
            }		
    }

    /**
      * 	EDIT ACTION
      */
    public function editAction()
    {
        $id = $this->_request->getParam("id");//Get id
        $this->view->data = $this->_Model->find_data( $id );//Get info
        
        if( $this->_request->isPost() )
        {
                $data = array(
                        "user_email" => "".$this->_request->getPost("user_email")."",
                        "user_address" => "".$this->_request->getPost("user_address")."",
                        "user_level" => "".$this->_request->getPost("user_level")."",
                        "user_phone" => "".$this->_request->getPost("user_phone")."",
                        "user_password" => "".$this->_request->getPost("user_password").""
                );
                $newModel->updateUser($data, $id);
                $this->_redirect("admin/user/index");	
        }
    }
}
